package com.dr.controller.admin;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dr.entity.Message;
import com.dr.entity.MessageVO;
import com.dr.service.MessageService;
import com.dr.util.LayUiResult;

@Controller
@RequestMapping("/admin/")
public class MessageController {
  @Autowired
  private MessageService messageService;
  /**
	 * 全查
	 * 
	 * @param p
	 * @return 以Map的方式返回查询到的数据和行数
	 */
	@RequestMapping(value = "/AllMessage.do", method = RequestMethod.GET)
	@ResponseBody // 转为json格式数据
	public Map<String, Object> AllMessage(MessageVO messageVO) {
		// 调用service
		List<Message> list = messageService.AllMessage(messageVO);
		int count = messageService.getCount(messageVO);
		// 返回结果都是以Map的方式返会给Layui所以返回值都是Map
		return list != null ? LayUiResult.toClient("0", "success", count, list) : LayUiResult.toClient("1", "没有数据");
	}
	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */

	@RequestMapping(value = "/deleteMessage.do", method = RequestMethod.POST)

	@ResponseBody
	public Map<String, Object> deleteMessage(int id) { // 调用service
		int res = messageService.deleteMessage(id); // 返回结果
		return res > 0 ? LayUiResult.toClient("0", "删除成功") : LayUiResult.toClient("1", "删除失败");

	}
	/**
	 * 修改
	 * 
	 * @param ids
	 * @return 返回
	 */
	@RequestMapping(value = "/updateMessage.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateMessage(Message m) {
		// 调用service
		int res = messageService.updateMessage(m);
		// 返回结果
		return res > 0 ? LayUiResult.toClient("0", "修改成功") : LayUiResult.toClient("1", "修改失败");
	}
	/**
	 * 修改是否回复（0没有，1回复了）
	 * 
	 * @param id
	 * @param valid
	 * @return
	 */

	@RequestMapping(value = "/updateisAnswer.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateisAnswer(int id, String valid) {
		int isAnswer = valid.equals("on") ? 1 : 0;
		// 调用service
		int res = messageService.updateisAnswer(id, isAnswer);
		// 返回结果
		return res > 0 ? LayUiResult.toClient("0", "修改成功") : LayUiResult.toClient("1", "修改失败");
	}
}
